<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller{
	
	function __construct(){
		parent::__construct();
	}

	public function index(){
		$this->load->view('register');
	}

	//添加新的用户
	public function add_user(){
		$email = $this->input->get_post('email');
		$userName = $this->input->get_post('userName');
		$password = $this->input->get_post('password');

		$file = './uploads/'.$userName;
		if(!file_exists($file)){
			mkdir($file,0777);
		}
		
		$userInfo = array(
			'username' => $userName,
			'email' => $email,
			'password' => md5($password)
		);

		$result = $this->UsersModel->add_user($userInfo);
		redirect('/index');
	}

	//检验邮箱是否存在
	public function ajax_check_email(){
		$email = $this->input->get_post('email');
		$result = $this->UsersModel->check_email_address($email);
		$json = array('success' =>($result && $email));
		echo json_encode($json);
		exit(0);
	}

	//检验用户名是否存在
	public function ajax_check_userName(){
		$userName = $this->input->get_post('userName');
		$result = $this->UsersModel->check_userName($userName);
		$json = array('success' =>($result && $userName));
		echo json_encode($json);
		exit(0);
	}
}